﻿
@{
    ViewData["Title"] = "UpdateMeasureUnitView";
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">单位编码</label>
            <div class="layui-input-block">
                <input type="text" id="unitCode" name="unitCode" lay-verify="required" placeholder="请输入单位编码" autocomplete="off" class="layui-input">
                <button type="button" class="layui-btn layui-bg-orange" id="generateId">生成雪花 ID</button>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">单位名称</label>
            <div class="layui-input-block">
                <input type="text" id="unitName" name="unitName" lay-verify="required" placeholder="请输入单位名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否是主单位</label>
            <div class="layui-input-block">
                <input type="radio" id="isNoUnit" name="isNoUnit" value="1" title="是" checked lay-filter="isNoUnit">
                <input type="radio" id="isNoUnit" name="isNoUnit" value="0" title="否" lay-filter="isNoUnit">
            </div>
        </div>




        <!-- 主单位相关项（动态添加部分） -->
        <div id="mainUnitFields" style="display:none;">
            <div class="layui-form-item">
                <label class="layui-form-label">主单位</label>
                <div class="layui-input-block">
                    <select id="primaryUnit" name="primaryUnit" lay-search>
                        <option value="0">请选择主单位</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">与主单位换算比例</label>
                <div class="layui-input-block">
                    <input type="text" id="conversion" name="conversion" placeholder="请输入换算比例（默认一比多少）" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input type="radio" id="isUsed" name="isUsed" value="1" title="是" checked>
                <input type="radio" id="isUsed" name="isUsed" value="0" title="否">
            </div>
        </div>


        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" id="remark" name="remark"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
            </div>
        </div>
    </form>

<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script src="~/libs/jquery/jquery.js"></script>


<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script src="~/libs/jquery/jquery.js"></script>





<script>
    // 页面加载完成后执行的函数
    $(function () {
        $('#primaryUnit').val(0);
        $('#conversion').val(1);
        getMainUnitList()
        mainUnitListOne();
    });

    //获取主数据进行反填
    function mainUnitListOne() {
        $.ajax({
            url: '/mainData/MeasureUnitOneView',
            type: 'get',
            data: {
                id: @ViewBag.Id
            },
            dataType: 'json',
            success: function (res) {
                $("#unitCode").val(res.data.unitCode);
                $("#unitName").val(res.data.unitName);
                $("#isNoUnit").val(res.data.isNoUnit);
                $("#primaryUnit").val(res.data.primaryUnit);
                $("#conversion").val(res.data.conversion);
                $("#isUsed").val(res.data.isUsed);
                $("#remark").val(res.data.remark);
                if (res.data.isNoUnit === 1) {
                    $('#mainUnitFields').hide();
                } else {
                    $('#mainUnitFields').show();
                }
                layui.form.render("select");
                layui.form.render("radio");
            }
        });
    }

    //获取主单位列表
    function getMainUnitList() {
        $.ajax({
            url: '/mainData/getPrimaryUnit',
            type: 'get',
            dataType: 'json',
            success: function (res) {
                console.log(res.data);
                if (res.code == 0) {

                var select = $('#primaryUnit'); // 获取 select 元素

                // 清空现有的选项（除了第一个默认选项）
                select.find('option:not(:first)').remove();
                // 动态添加选项
                $.each(res.data, function (key, value) {
                    select.append('<option value="' + value.id + '">' + value.unitName + '</option>');
                });
                layui.form.render('select');
                }
            }
        })
    }

    layui.use(['form', 'laydate', 'util'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var util = layui.util;

       // 监听是否主单位的选择
       form.on('radio(isNoUnit)', function (data) {
       var value = data.value; // 获取选中的值
       console.log(value);
        if (value === '1') {
            // 如果是主单位，隐藏额外的字段
            $('#mainUnitFields').hide();


        } else {
            // 如果不是主单位，显示额外的字段
            $('#mainUnitFields').show();
            $('#conversionRatio').val("");
            $('#conversion').val("");
        }
    });

      // 提交事件
       form.on('submit(demo1)', function(data){
           var field = data.field; // 获取表单字段值
           if (isNaN(parseFloat(field.conversion))) {
               layer.alert('请输入有效的换算比例哦！亲', {
                   skin: 'layui-layer-molv' // 样式类名
                 }); 
               return false; // 阻止表单跳转
           
           };
           field.id=@ViewBag.Id;
           field.isNoUnit=parseInt(field.isNoUnit);
           field.primaryUnit=parseInt(field.primaryUnit);
           field.isUsed=parseInt(field.isUsed);
           field.conversion=parseInt(field.conversion);
           console.log(field);
           $.ajax({
               url:'/mainData/UpdateMeasureUnit',
               type:'post',
               data:field,
               dataType:'json',
               success:function(res){
                   console.log(res);
                   if(res.code==1){
                       layer.alert('修改成功！')
                       setTimeout(function(){closeWorkstation();},500)
                   }
                   else if(res.code==-1){
                       layer.alert('修改失败！,编号或名称已存在！')
                   }
               }
           })
           return false; // 阻止默认 form 跳转
       });
    });


    //关闭弹框
    function closeWorkstation() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前的索引
        parent.layer.close(index); //再执行关闭
    }


    //获取雪花ID
    function generateSnowflakeId() {
    return  Date.now()  + Math.floor(Math.random() * 1000);
    }

    // 点击生成雪花 ID 按钮
    document.getElementById('generateId').addEventListener('click', function() {
         event.preventDefault(); // 阻止默认行为
         var snowflakeId = generateSnowflakeId();// 生成雪花 ID
         document.getElementById('unitCode').value = snowflakeId;// 更新输入
    });


</script>

